import { useState, useEffect } from 'react'

function Son() {
  useEffect(() => {
    const timer = setInterval(() => {
      console.log('执行定时器')
    }, 1000)
    // 清除副作用（组件卸载时执行）
    return () => {
      clearInterval(timer)
    }
  }, [])
  return <div>son</div>
}
function App() {
  const [show, setShow] = useState(true)
  return (
    <>
      <div style={{ width: '500px', height: '500px', border: '1px solid green' }}>
        {show && <Son></Son>}
        <button onClick={() => setShow(false)}>卸载子组件</button>
      </div>
    </>
  )
}

export default App
